package hc.bre.project

import hc.bre.ComponentConfig
import hc.bre.ComponentRegistry
import hc.bre.ThisApplicationConfig
import hc.bre.cmn.struct.RuleResultStruct
import hc.bre.rest.admin.ProjectEntry
import hc.bre.rest.admin.ScriptFileMode
import hc.bre.cmn.*

@groovy.transform.CompileStatic
class ProjectDefinition extends RuleDefinition{
	public static final ComponentConfig _config ;
	static {
		_config = new ComponentConfig();
		_config.rtype = 'project';
		_config.rname = '决策项目';
		_config.builderClass = ProjectBuilder;
		_config.resultStruct = new RuleResultStruct();
		_config.homepage = '/rule/projecthome.htm';
		_config.objectpage = '/rule/projecteditor.htm?rpath=${rpath}';
		_config.description = '''
项目是决策规则的管理容器，一个项目内的所有组件互相配合完整一个整体决策任务。
		''';
	}

	//avoid the @ToString recursive call from other rule reference to it.
	@Override
	public String toString() {
		return rpath;
	}

	String defaultEntry;
	List<String> list = [];
	List<RuleDefinition> ruleList ;

	@Override
	public ProjectDefinition load(Map map) {
		super.load(map);
		
		defaultEntry = map['defaultEntry'];
		list = hc.Util.loadValueList(map.list, String);

		//come from user upload
		List<Map> lst = map.ruleList as List<Map>;
		lst?.each{
			if(ruleList == null) ruleList=[];
			ruleList.add (RuleDefinition.loadRuleDef(it));
		}

		return this;
	}

	@Override
	public DiffResult<RuleDefinition> compareTo(RuleDefinition other1) {
		DiffResult<RuleDefinition> result = super.compareTo(other1);
		ProjectDefinition other = other1 as ProjectDefinition

		List<DiffPair> list = DiffPair.buildPairList(ruleList,other.ruleList);
		list.each {
			if(it.right == null)
			{
				result.add(DiffType.created, it.left, it.right);
			}else if(it.left == null)
			{
				result.add(DiffType.deleted, null, it.right);
			}
			else
			{
				result.addIfDiff(it.left.compareTo(it.right));
			}
		}

		//		LinkedHashMap<String,RuleDefinition> otherRuleList = new LinkedHashMap<String,RuleDefinition>()
		//		other.ruleList?.each {
		//			otherRuleList.put(it.rpath, it);
		//		}
		//
		//		ruleList?.each {
		//			RuleDefinition ot = otherRuleList.get(it.rpath);
		//			if(ot){
		//				otherRuleList.remove(it.rpath);
		//				result.addIfDiff(it.compareTo(ot));
		//			}else {
		//				result.add(DiffType.created, it, ot);
		//			}
		//		}
		//
		//		otherRuleList?.values?.each {
		//			result.add(DiffType.deleted, null, it);
		//		}
		//
		return result;
	}

//	@com.fasterxml.jackson.annotation.JsonIgnore
//	Map<Integer, RuleDefinition> ruleIdMap;
//	public Map<Integer, RuleDefinition> getRuleIdMap() {
//		if(ruleList == null) throw new Exception('the rule list or map is not loaded.');
//
//		if(ruleIdMap == null)
//		{
//			ruleIdMap =[:];
//			ruleList.each {
//				ruleIdMap.put(it.rid, it);
//			}
//		}
//		return ruleIdMap;
//	};

	@com.fasterxml.jackson.annotation.JsonIgnore
	Map<String,RuleDefinition> rulePathMap;
	public Map<String, RuleDefinition> getRulePathMap() {
		if(ruleList == null) throw new Exception('the rule list or map is not loaded.');

		if(rulePathMap == null)
		{
			rulePathMap =[:];
			ruleList.each {
				rulePathMap.put(it.rpath, it);
			}
		}
		return rulePathMap;
	}
}
